package com.example.myapplication.ui;

import androidx.appcompat.app.AppCompatActivity;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.example.myapplication.R;
import com.example.myapplication.tools.ToastUtils;
import com.google.gson.Gson;
import com.google.zxing.integration.android.IntentIntegrator;
import com.google.zxing.integration.android.IntentResult;

public class MainActivity extends Activity {

    Context context;
    TextView tvBook,tvLib;
    boolean isBookISBN = false;//true 是图书ISBN  ，false 是馆内条码

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        context = this;
        tvBook= findViewById(R.id.tv_result_show);
        tvLib = findViewById(R.id.tv_result_lib_show);
    }

    public void scanningBook(View v){
        isBookISBN = true;
        scanning();
    }

    public void scanninglibrary(View v){
        isBookISBN = false;
        scanning();
    }
    private void scanning(){
        IntentIntegrator integrator = new IntentIntegrator(this);
        // 设置要扫描的条码类型，ONE_D_CODE_TYPES：一维码，QR_CODE_TYPES-二维码
        if(isBookISBN){
            integrator.setDesiredBarcodeFormats(IntentIntegrator.ONE_D_CODE_TYPES);//一维码
        }else{
            integrator.setDesiredBarcodeFormats(IntentIntegrator.QR_CODE_TYPES);//二维码
        }
//        integrator.setDesiredBarcodeFormats(IntentIntegrator.ONE_D_CODE_TYPES);//一维码
//		integrator.setDesiredBarcodeFormats(IntentIntegrator.QR_CODE_TYPES);//二维码
        integrator.setCaptureActivity(ScanActivity.class); //设置打开摄像头的Activity
        integrator.setPrompt("请扫描ISBN"); //底部的提示文字，设为""可以置空
        integrator.setCameraId(0); //前置或者后置摄像头
        integrator.setBeepEnabled(true); //扫描成功的「哔哔」声，默认开启
        integrator.setBarcodeImageEnabled(true);
        integrator.initiateScan();
    }

    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        IntentResult scanResult = IntentIntegrator.parseActivityResult(requestCode, resultCode, data);
        if (scanResult != null) {
            String result = scanResult.getContents();
            ToastUtils.showMessages(result,context);
            if(isBookISBN){
                tvBook.setText("条形码:" + result);
            }else{
                tvLib.setText("二维码:" + result);
            }

        }
    }

}